<template>
  <div class="box">
    <el-card class="box-card">
      <span>数据列表</span>
      <el-button type="primary" @click="addData" class="add"
        >增加数据</el-button
      >
    </el-card>
    <el-table
      highlight-current-row
      :data="tableData"
      stripe
      style="width: 100%"
    >
      <el-table-column prop="author" label="作者"> </el-table-column>
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="desc" label="文章描述">
        <template slot-scope="scope">
          <div>{{ scope.row.desc.substr(0, 10) + "..." }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="thumb" label="文章封面">
        <template slot-scope="scope">
          <img :src="scope.row.thumb" />
        </template>
      </el-table-column>
      <el-table-column prop="zan" label="Like">
        <template slot-scope="scope">
          <el-tag type="danger">{{ scope.row.zan }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createAt" label="上传日期">
        <template slot-scope="scope">
          {{ getTime(scope.row.createAt) }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="
              {
                delItem(scope);
              }
            "
            circle
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="
              {
                edit(scope.row.id);
              }
            "
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="10"
      :current-page="page"
      @current-change="pageChange"
      :hide-on-single-page="true"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import * as dayjs from "dayjs";
import { fetchCate, delArt } from "@api";
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      page: 1,
      pageSize: 10,
    };
  },
  created() {
    //初始化请求第一页数据

    if (sessionStorage.getItem("page")) {
      this.page = Number(sessionStorage.getItem("page"));
    }
    //请求第一页数据
   this.getFetchCate()
  },
  methods: {

  getFetchCate(){
   fetchCate({
      pageSize: this.pageSize,
      page: this.page,
    }).then((res) => {
      if (res.data.code === 200) {
        this.total = res.data.data.total
          this.tableData = [...this.tableData,...res.data.data.lists]
       
      }
    })
  },
    

    //添加数据  跳转到添加数据页
    addData() {
      this.$router.push("/home/datalists/adddata");
    },



    //将毫秒数转换为日期格式
    getTime(val) {
      return dayjs(val).format("YYYY-MM-DD");
    },



    delItem(info) {
      //删除某条数据
      console.log(info);
      this.tableData.splice(info.$index, 1);
      delArt(info.row.id).then((res) => {
        if (res.data.code === 200) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
        }
      });
    },

    pageChange(val) {
      //点击分页功能时触发
     this.pageSize=val
   this.getFetchCate()
    },

    //点击点击跳转到编辑页
    edit(id) {
      this.$router.push({
        path: "/home/datalists/dataedit",
        query: { id: id },
      });
    },
  },
};
</script>
<style scoped>
.box-card {
  width: 99.9%;
  text-align: left;
  font-weight: 600;
  font-size: 24;
  height: 130px;
}
.box-card span {
  margin-left: 30px;
}
.el-pagination {
  margin-top: 20px;
}
.add {
  float: right;
  margin-right: 100px;
  margin-top: 60px;
}
</style>